package com.wf.smartcomm.server.serviceImpl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Message;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.MessageService;
import com.wf.smartcomm.server.util.IdUtils;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-20 下午7:48:34 
 * 
 */
@Service
@Transactional
public class MessageServiceImpl implements MessageService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(Message message) {
		message.setObjectId(IdUtils.uuid());
		message.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().persist(message);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByUser(User user, Page page) {
		Query query = factory.getCurrentSession().createQuery("from Message where toId = :uid order by createdAt desc");
		query.setParameter("uid", user.getObjectId());
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	public void listByVillage(Village village, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Message where village = :village");
		query.setParameter("village", village);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Message where village = :village order by createdAt");
		query.setParameter("village", village);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Message get(String id) {
		String hql = "from Message where objectId =:id";
		Query query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("id", id);
		return (Message) query.uniqueResult();
	}

	@Override
	public void delete(String messageId) {
		Query query = factory.getCurrentSession().createSQLQuery("delete from t_message where objectId =:messageId");
		query.setParameter("messageId", messageId);
		query.executeUpdate();
	}

	@Override
	public void deleteAll(User user) {
		Query query = factory.getCurrentSession().createSQLQuery("delete from t_message where toId = :userId");
		query.setParameter("userId", user.getObjectId());
		query.executeUpdate();
	}

	@Override
	public void readMsg(String messageId) {
		Query query = factory.getCurrentSession().createSQLQuery("update t_message set status = 1 where objectId = :messageId");
		query.setParameter("messageId", messageId);
		query.executeUpdate();
	}

	@Override
	public Integer countUnRead(User user) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Message where toId =:userId and status = 0");
		query.setParameter("userId", user.getObjectId());
		Long counts = (Long) query.uniqueResult();
		if (counts != null) {
			return counts.intValue();
		}
		return 0;
	}

}
